package other;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @description：MD5加密
 * @author： HD
 * @time：2017/8/21
 */

public class MD5Util {

    protected static char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    protected static MessageDigest messagedigest = null;

    public MD5Util() {
    }

    public static String getMD5String(String value) {
        return value == null ? null : getMD5String(value.getBytes());
    }

    public static String getFileMD5String(String fileName) {
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(fileName);
            byte[] e = new byte[4096];
            boolean numRead = false;

            int numRead1;
            while((numRead1 = fis.read(e, 0, 4096)) > 0) {
                messagedigest.update(e, 0, numRead1);
            }
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e1) {
            e1.printStackTrace();
        }finally {
            if(fis != null) {
                try {
                    fis.close();
                    fis = null;
                } catch (IOException var9) {
                    var9.printStackTrace();
                }
            }

        }

        return bufferToHex(messagedigest.digest());
    }

    public static String getFileMD5String(File file) throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            byte[] e = new byte[4096];
            boolean numRead = false;

            int numRead1;
            while((numRead1 = fis.read(e, 0, 4096)) > 0) {
                messagedigest.update(e, 0, numRead1);
            }
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e1) {
            e1.printStackTrace();
        }finally {
            if(fis != null) {
                try {
                    fis.close();
                    fis = null;
                } catch (IOException var9) {
                    var9.printStackTrace();
                }
            }

        }

        return bufferToHex(messagedigest.digest());
    }

    public static String getFileMD5String_old(File file) throws IOException {
        FileInputStream in = new FileInputStream(file);
        FileChannel ch = in.getChannel();
        MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0L, file.length());
        messagedigest.update(byteBuffer);
        return bufferToHex(messagedigest.digest());
    }

    public static boolean checkPassword(String password, String md5PwdStr) {
        String s = getMD5String(password);
        return s.equals(md5PwdStr);
    }

    public static synchronized String getMD5String(byte[] bytes) {
        try {
            messagedigest.reset();
            messagedigest.update(bytes);
            return bufferToHex(messagedigest.digest());
        } catch (NullPointerException var2) {
            var2.printStackTrace();
            return null;
        }
    }

    private static String bufferToHex(byte[] bytes) {
        return bytes.length == 0 ? "" : bufferToHex(bytes, 0, bytes.length);
    }

    private static String bufferToHex(byte[] bytes, int m, int n) {
        StringBuffer stringbuffer = new StringBuffer(2 * n);
        int k = m + n;

        for (int l = m; l < k; ++l) {
            appendHexPair(bytes[l], stringbuffer);
        }

        return stringbuffer.toString();
    }

    private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
        char c0 = hexDigits[(bt & 240) >> 4];
        char c1 = hexDigits[bt & 15];
        stringbuffer.append(c0);
        stringbuffer.append(c1);
    }

    public static String md5Crypto(String src) {
        char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

        try {
            byte[] e = src.getBytes();
            messagedigest.reset();
            messagedigest.update(e);
            byte[] md = messagedigest.digest();
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;

            for (int i = 0; i < j; ++i) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 15];
                str[k++] = hexDigits[byte0 & 15];
            }

            return new String(str);
        } catch (Exception var9) {
            var9.printStackTrace();
            return null;
        }
    }

    static {
        try {
            messagedigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException var1) {
            var1.printStackTrace();
        }

    }


}
